#ifndef __IAP_H__
#define __IAP_H__

#include "MD5.h"

#include <stdbool.h>

typedef  void (*iapfun)(void);				//定义一个函数类型参数

#define  DI()      { __disable_irq();  }
#define  EI()      { __enable_irq();   }

#define  MCU_SoftRest()      { NVIC_SystemReset(); }  

en_result_t IAP_JumpToApp(uint32_t u32Addr);

void IAP_ResetConfig(void);

void IAP_start_check(void);


/* pack固件包对象结构体*/
typedef struct
{   
	uint8_t   buf_hex[128];
	uint16_t  total_num; /* 包总数:以128字节为1包*/
	uint16_t  index;     /*当前下发的包序号,从1开始*/
	uint8_t   MD5_hex[16];  /* 备份区固件计算出来的md5*/
	uint32_t  log[6];       /* 记录固件包接收状态,按bit位,小端模式*/
}OTA_pack_t;

extern OTA_pack_t pack;

typedef enum 
{
	jump2app,
	jump2boot,
}en_bootloderJump;


typedef enum 
{
	ready_boot,
	flash_wateraddr_empty,
	ready_api,
}boot_state;


typedef enum 
{
	Ack_ready_OK,
	Ack_update_status,
	Ack_md5_OK,
	Ack_md5_Error,
}iap_ack_type;


//与升级相关的宏定义
//定义30k 以后的地址为备份区

/* APP flash相关宏定义 */
#define  BACKUP_START_ADDR   		(0x00008800) /*34KB*/
#define  APP_MAX_SIZE   			(0x6800)  /*26kb*/
#define  APP_ADDRESS                (FLASH_BASE + BOOT_SIZE)          //APP程序存储基地址0x2000

#define FLASH_IAP_OPREAT_FLAG       ((uint32_t)0x67890123)            //从BootLoader para区读到此值，表示APP需要更新

/* BootLoader flash相关宏定义 */
#define  BOOT_SIZE                   (16*FLASH_SECTOR_SIZE)             //BootLoader flash尺寸8kb
#define  BOOT_PARA_ADDRESS        	 (Sector125_BaseAddr)

void IAP_UpdateCheck(uint8_t *state);
void IAP_ChangeMode(en_bootloderJump which_jump);

void IAP_WriteFirmware2Backup(uint32_t dest_addr, uint8_t *buff);/*写固件到备份区*/
void IAP_WriteFirmware2Flash( uint32_t sectors_num); /*搬运固件到APP*/

#ifdef APP

void IAP_Flash_EmptyCheck(void);

void IAP_frame_dl645_parse(void *frame_recv);

void IAP_Ack_Start_Ready(void);
void IAP_Ack_MD5Status(iap_ack_type status);
bool IAP_firmware_MD5_Checkout(uint8_t *md5_InputValue);

void IAP_Ack_End_OK(bool packlost_status);

#endif

#endif







































